import random
from multiprocessing import Pipe

import pandas

from gcz_common.config import config
from gcz_common.core.analyse import *
from gcz_common.utils.thread.ThreadUtil import ThreadUtil
from gcz_common.utils.process.ProcessUtil import ProcessUtil
from gcz_pyqt.views.UIHomeView import *
from gcz_spider.spider import startSpider
from gcz_common.core.dispatcher import DispatcherBase


class HomeController:
    def __init__(self):
        self.ui = UIHomeView()
        self.ui.showMaximized()
        self.ui.scrapyPBClickCallback = self.scrapyPBClickCallback
        self.ui.clearTablePBClickCallback = self.clearTablePBClickCallback
        self.ui.loadLocalDataPBClickCallback = self.loadLocalDataPBClickCallback
        self.ui.createChartPBClickCallback = self.createChartPBClickCallback
        self.ui.chartParamsPBClickCallback = self.chartParamsPBClickCallback
        self.ui.comboCheckBoxClickCallback = self.comboCheckBoxClickCallback

        self.process = ProcessUtil(config.log.statementShow)
        self.thread = ThreadUtil(config.log.statementShow)

        self.dispatch = DispatcherBase.getInstance()

        self.dispatch.add("datalist", self.phoneDatasRecv)

        self.original = []
        self.chartsParams = {'pie': 1, 'bar': 1, 'line': 1}
        self.priceCount = None

        self.analyseNationData = [[], []]
        self.analyseScoreData = [[], [], []]

    # region 委托
    def phoneDatasRecv(self, datalist):
        self.original=datalist
        result = parse(self.original)
        self.ui.setUI(result)
        for data in datalist:
            self.ui.setTableUI(data)
    # endregion

    # region 按钮点击回调
    def scrapyPBClickCallback(self, statue):
        if statue: self.thread.startThread(startSpider)
        else: self.thread.stopThread()

    def clearTablePBClickCallback(self):
        self.original = []

    def loadLocalDataPBClickCallback(self):
        if not os.path.exists(f'{config.file.csvPath}{config.file.phoneDataFileName}.xls'):
            self.ui.printLog(f"无本地数据，导入失败！")
            return
        try:
            data = pandas.read_excel(f'{config.file.csvPath}{config.file.phoneDataFileName}.xls')
            data = eval(data.to_json(orient="values", force_ascii=False))
            self.original = data
            result = parse(self.original)
            self.ui.setUI(result)
            self.createChartPBClickCallback()
        except Exception as e:
            print(e)
        def fun():
            for item in data:
                self.ui.setTableUI(item)
        self.thread.startThread(fun)
    def createChartPBClickCallback(self):
        analyseNation()
        analyseScore()
        self.ui.setPyEchartUI([1, 3])

    def chartParamsPBClickCallback(self, type, params):
        types = ['pie', 'bar', 'line', "饼图", "柱状图", "折线图"]

        self.chartsParams[types[type-1]] = params

    def comboCheckBoxClickCallback(self, index, data):
        if index[0] == 1:
            self.analyseNationData[index[1] - 1] = data
            analyseNation(self.analyseNationData)
            self.ui.setPyEchartUI([3])
        else:
            self.analyseScoreData[index[1] - 1] = data
            analyseScore(self.analyseScoreData)
            self.ui.setPyEchartUI([1])
    # endregion


